package shop.yaojianfeng.springlearning.section6.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import shop.yaojianfeng.springlearning.section6.entity.Account;
import shop.yaojianfeng.springlearning.section6.service.AccountService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 简单账户表(Account)表控制层
 *
 * @author
 * @since 2022-04-27 16:35:23
 */
@RestController
@RequestMapping("account")
public class AccountController {
    /**
     * 服务对象
     */
    @Autowired
    @Qualifier("accountService")
    private AccountService accountService;


    /**
     * 账户转入
     *
     * @param money
     * @param name
     * @return
     */
    @RequestMapping("/out")
    public Account out(int money, String name) {
        Account param = new Account();
        param.setName(name);
        param.setMoney(money);
        Account result = accountService.out(param);
        return result;
    }

    /**
     * 账户转出
     *
     * @param money
     * @param name
     */
    @RequestMapping("/in")
    public Account in(int money, String name) {
        Account param = new Account();
        param.setName(name);
        param.setMoney(money);
        Account result = accountService.in(param);
        return result;
    }

    /**
     * 两个账户之间进行转账
     * @param firstName
     * @param secondName
     * @param money
     * @return
     */
    @RequestMapping("/transfer")
    public List<Account> transfer(String firstName, String secondName, int money) {
        List<Account> list = new ArrayList<>();
        Account out = this.out(money, firstName);
        list.add(out);

        //使用异常中断程序
        System.out.println(10/0);
        Account in = this.in(money, secondName);
        list.add(in);
        return list;
    }


}

